package com.ewanghuiju.app.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.ewanghuiju.app.R;

import java.lang.reflect.Field;

import androidx.appcompat.widget.AppCompatEditText;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/14
 */

public class EditTextWithDel extends AppCompatEditText implements View.OnFocusChangeListener {

    private Drawable imgAble;
    private Context mContext;
    //点击区域大小
    private int clickArea = 100;
    //是否为密码类型
    private boolean isPassword = false;
    //当前是否可查看密码
    private boolean isEyeOne = false;


    public EditTextWithDel(Context context) {
        super(context);
        mContext = context;
        init();
//      this(context,null);
    }

    public EditTextWithDel(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
//      this(context,attrs,0);
    }

    public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }


    private void init() {
        setHintTextColor(getResources().getColor(R.color.color_999));
        setTextColor(getResources().getColor(R.color.color_333));
        setGravity(Gravity.CENTER_VERTICAL);
        setCursorColor();
        imgAble = mContext.getResources().getDrawable(R.mipmap.btn_equip_clear);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setDrawable();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        setOnFocusChangeListener(this);
    }

    //设置删除图片
    public void setDrawable() {
        if (hasFocus()) {
            if (length() < 1) {
                setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

            } else {
                setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
            }
        } else {
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }

    }

    // 处理右边图标点击事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (imgAble != null && event.getAction() == MotionEvent.ACTION_DOWN && hasFocus()) {
            int eventX = (int) event.getRawX();
            int eventY = (int) event.getRawY();
            Rect rect = new Rect();
            int[] location = new int[2];
            getLocationOnScreen(location);
            rect.right = location[0] + getWidth();
            rect.left = rect.right - clickArea;
            rect.top = location[1];
            rect.bottom = rect.top + getHeight();
            if (rect.contains(eventX, eventY)) {//点击图标
                if (isPassword) {//密码类型
                    if (isEyeOne) {
                        setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        isEyeOne = false;
                    } else {
                        setInputType(InputType.TYPE_CLASS_TEXT);
                        isEyeOne = true;
                    }
                } else {//普通类型 删除文字
                    setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {//有焦点
            setDrawable();
            ((EditTextWithDel) view).setSelection(((EditTextWithDel) view).getText().length());
        } else {//失去焦点
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }

    public int getClickArea() {
        return clickArea;
    }

    /**
     * 设置点击区域大小
     *
     * @param clickArea
     */
    public void setClickArea(int clickArea) {
        this.clickArea = clickArea;
    }


    /**
     * 设置光标颜色值
     */
    public void setCursorColor() {
        try {
            Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
            f.setAccessible(true);
            f.set(this, R.drawable.shape_cursor_color);
        } catch (Exception ignored) {
        }
    }


    @Override
    public void setInputType(int type) {
        super.setInputType(type);
        if (type == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)) {
            isPassword = true;
            imgAble = mContext.getResources().getDrawable(R.mipmap.btn_equip_setting_eye_off);
        } else {
            if (isPassword) {
                imgAble = mContext.getResources().getDrawable(R.mipmap.btn_equip_setting_eye_on);
            }
        }
        setDrawable();
    }
}